#include "ISNW_Font.h"
using namespace ISNW;

namespace ISNW
{
	ISNW_Font* g_pFont = ISNW_NULL;
}

ISNW_Font::ISNW_Font()
:m_hfont(ISNW_NULL)
,m_hdc(ISNW_NULL)
,m_d3dfont(ISNW_NULL)
{
}

ISNW_VOID ISNW_Font::InitFont()
{
	g_pFont = GetPtr();
	GetPtr()->Init();
}

ISNW_Font::~ISNW_Font()
{
	Release();
}

ISNW_Font* ISNW_Font::GetPtr()
{
	static ISNW_Font sInstance;
	return &sInstance;
}

ISNW_VOID ISNW_Font::Init()
{
	m_hdc = CreateCompatibleDC(NULL);
	m_hfont = CreateFont(0,0,0,0,FW_BOLD,FALSE,FALSE,FALSE,DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,
		DEFAULT_PITCH|FF_DONTCARE,"Arial");
	SelectObject(m_hdc,m_hfont);

	HRESULT hr = S_OK;
	hr = D3DXCreateFont(g_pdevice,0,0,FW_BOLD,1,FALSE,DEFAULT_CHARSET,
		OUT_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_DONTCARE,
					"Arial",&m_d3dfont);
	if(FAILED(hr)){
		switch(hr){
			case D3DERR_INVALIDCALL:
				MessageBox(g_pApp->GetWnd(),"Invalid call","D3D font error",MB_OK);
				break;
			case D3DXERR_INVALIDDATA:
				MessageBox(g_pApp->GetWnd(),"Invalid data","D3D font error",MB_OK);
				break;
			case E_OUTOFMEMORY:
				MessageBox(g_pApp->GetWnd(),"Out of memeory","D3D font error",MB_OK);
				break;
			default:
				MessageBox(g_pApp->GetWnd(),"Create D3D Font failed","D3D error",MB_OK);
				break;
		}
	}
}

ISNW_VOID ISNW_Font::Create()
{
	Release();
	Init();
}

ISNW_VOID ISNW_Font::Release()
{
	if(m_hfont){
		DeleteObject(m_hfont);
		m_hfont = ISNW_NULL;
	}
	if(m_hdc){
		DeleteDC(m_hdc);
		m_hdc = ISNW_NULL;
	}
}

ISNW_VOID ISNW_Font::DrawText(char* p,ISNW_INT count,RECT rect)
{
	g_pSprite->GetSprite()->Begin(D3DXSPRITE_ALPHABLEND);
	m_d3dfont->DrawText(g_pSprite->GetSprite(),p,count,&rect,0,D3DCOLOR_XRGB(0xff,0x00,0x00));
	g_pSprite->GetSprite()->End();
}